package com.fyt.reachgo.controller;

import com.fyt.reachgo.domain.Result;
import com.fyt.reachgo.domain.dto.LoginDTO;
import com.fyt.reachgo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/login")
@RestController
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("/sendCode")
    public Result sendCode(@RequestBody Map<String, String>phone) {
        String mobile = phone.get("mobile");
        log.debug("手机号：" + mobile);
        if (mobile == null || mobile.length() != 11) {
            return Result.error("手机号格式不正确！");
        }
        return userService.sendCode(mobile);
    }

    /**
     * 根据手机号登录 / 注册
     * @param loginDTO
     * @return
     */
    @PostMapping("/loginByMobile")
    public Result loginByMobile(@RequestBody LoginDTO loginDTO) {
        return userService.loginByMobile(loginDTO);
    }

    /**
     * 根据密码登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/loginByPassword")
    public Result loginByPassword(@RequestBody LoginDTO loginDTO) {
        return userService.loginByPassword(loginDTO);
    }
}
